home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / CLIP.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  8KB  |  302 lines

  1. /*
  2.  * CLIP.C
  3.  *
  4.  * Functions to interact with the clipboard and one to enable or disable
  5.  * menu items depending on the state of the clipboard.
  6.  *
  7.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  8.  *
  9.  */
  10.  
  11. #include <windows.h>
  12. #include <ole.h>
  13. #include "oclient.h"
  14. #include "blackbox.h"
  15. #include "patron.h"
  16.  
  17.  
  18.  
  19.  
  20. /*
  21.  * MenuClipboardEnable
  22.  *
  23.  * Purpose:
  24.  *  Enabled or disables the Edit menu command of Cut, Copy, and Paste.
  25.  *  the OLE formats are handled through a call to MenuOLEClipboardEnable,
  26.  *  which occurs first to allow this function to enable Paste if there
  27.  *  still is a non-OLE format we can paste.  Note, however, that in this
  28.  *  application we do not paste anything but OLE objects.
  29.  *
  30.  *  Call this function from the the WM_INITPOPUPMENU message case for the
  31.  *  main window.
  32.  *
  33.  * Parameters:
  34.  *  hMenu           HMENU of the Edit popup menu.
  35.  *  pGlob           LPGLOBALS to the global variable block.
  36.  *
  37.  * Return Value:
  38.  *  None
  39.  */
  40.  
  41. void FAR PASCAL MenuClipboardEnable(HMENU hMenu, LPGLOBALS pGlob)
  42.     {
  43.     WORD        wTemp;
  44.  
  45.     //Cannot cut, copy, or clear without an active window.
  46.     wTemp=(NULL!=pGlob->hWndLastActive) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  47.     EnableMenuItem(hMenu, IDM_EDITCOPY,            wTemp | MF_BYCOMMAND);
  48.     EnableMenuItem(hMenu, IDM_EDITCUT,             wTemp | MF_BYCOMMAND);
  49.     EnableMenuItem(hMenu, IDM_EDITCONVERTTOSTATIC, wTemp | MF_BYCOMMAND);
  50.     EnableMenuItem(hMenu, IDM_EDITCLEAR,           wTemp | MF_BYCOMMAND);
  51.     EnableMenuItem(hMenu, IDM_EDITCLEARALL,        wTemp | MF_BYCOMMAND);
  52.  
  53.     /*
  54.      * If we pasted anything else, then we would check for those formats
  55.      * here and enable the Paste menu item if they were available.  Since
  56.      * we don't, then we don't mess with the Paste item.
  57.      */
  58.     return;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64.  
  65. /*
  66.  * FEditCut
  67.  *
  68.  * Purpose:
  69.  *  Places a copy of an object in the currently active window on the
  70.  *  clipboard then deletes that object and its window.
  71.  *
  72.  * Parameters:
  73.  *  pGlob           LPGLOBALS to the global variable block.
  74.  *
  75.  * Return Value:
  76.  *  BOOL            TRUE if successful, FALSE otherwise.
  77.  *
  78.  */
  79.  
  80. BOOL FAR PASCAL FEditCut(LPGLOBALS pGlob)
  81.     {
  82.     //Copy editor contents to the clipboard.
  83.     if (!FEditCopy(pGlob, TRUE))
  84.         return FALSE;
  85.  
  86.     //Delete the last active window, which is the one we copy.
  87.     SendMessage(pGlob->hWnd, WM_COMMAND, IDM_EDITCLEAR, 0L);
  88.     return TRUE;
  89.     }
  90.  
  91.  
  92.  
  93.  
  94. /*
  95.  * FEditCopy
  96.  *
  97.  * Purpose:
  98.  *  Places a copy of an object in the currently active window on the
  99.  *  clipboard.
  100.  *
  101.  * Parameters:
  102.  *  pGlob           LPGLOBALS to the global variable block.
  103.  *  fCut            BOOL indiciating if we are being used from FEditCut.
  104.  *
  105.  * Return Value:
  106.  *  BOOL            TRUE if anything was successfully placed on the
  107.  *                  clipboard, FALSE otherwise.
  108.  *
  109.  */
  110.  
  111. BOOL FAR PASCAL FEditCopy(LPGLOBALS pGlob, BOOL fCut)
  112.     {
  113.     LPOBJECT            pObj;
  114.     OLESTATUS           os;
  115.  
  116.     //Attempt to gain clipboard ownership.
  117.     if (!OpenClipboard(pGlob->hWnd))
  118.         return FALSE;
  119.  
  120.     //Copying an object to the clipboard could not be easier.
  121.     pObj=(LPOBJECT)SendMessage(pGlob->hWndLastActive, BBM_POBJECTGET, 0, 0L);
  122.     os=OleCopyToClipboard(pObj->pObj);
  123.  
  124.     //Free clipboard ownership.
  125.     CloseClipboard();
  126.  
  127.     return (OLE_OK==os);
  128.     }
  129.  
  130.  
  131.  
  132.  
  133. /*
  134.  * FEditPaste
  135.  *
  136.  * Purpose:
  137.  *  Retrieves an embedded or linked object from the clipboard and sets
  138.  *  it as the OLE object for a newly created window.
  139.  *
  140.  *  Note that if this function is called, then the clipboard format
  141.  *  is available because the Paste menu item is only enabled if the
  142.  *  format is present.
  143.  *
  144.  * Parameters:
  145.  *  pGlob           LPGLOBALS to the global variable block.
  146.  *  fLink           BOOL indicates if we want to Paste a linked object
  147.  *                  instead of an embedded object.
  148.  *  pDoc            LPDOCUMENT containing OLE information.
  149.  *
  150.  * Return Value:
  151.  *  BOOL            TRUE if successful, FALSE otherwise.
  152.  *
  153.  */
  154.  
  155. BOOL FAR PASCAL FEditPaste(LPGLOBALS pGlob, BOOL fLink, LPDOCUMENT pDoc)
  156.     {
  157.     HWND            hWndT;
  158.     FILEOBJECT      fo;
  159.     OLESTATUS       os;
  160.     LPOBJECT        pObj;
  161.     BOOL            fTemp;
  162.  
  163.  
  164.     if (!OpenClipboard(pGlob->hWnd))
  165.         return FALSE;
  166.  
  167.  
  168.     FileObjectDefaults(&fo);
  169.     pObj=PObjectAllocate(&fTemp, pDoc);
  170.  
  171.     if (!fTemp)
  172.         {
  173.         PObjectFree(pDoc, pObj);
  174.         CloseClipboard();
  175.         return FALSE;
  176.         }
  177.  
  178.  
  179.     if (fLink)
  180.         {
  181.         //Attempt to create a linked object.
  182.         os=OleCreateLinkFromClip(PSZOLE(IDS_STDFILEEDITING), (LPOLECLIENT)pObj,
  183.                                  pDoc->lh, fo.szName, &pObj->pObj, olerender_draw, 0);
  184.  
  185.         os=OsError(os, pDoc, pObj, TRUE);
  186.  
  187.         //On failure we'll try to create an embedded object.
  188.         if (OLE_OK!=os)
  189.             fLink=FALSE;
  190.         }
  191.  
  192.     if (!fLink)
  193.         {
  194.         //Attempt to create the embedded object.
  195.         os=OleCreateFromClip(PSZOLE(IDS_STDFILEEDITING), (LPOLECLIENT)pObj,
  196.                              pDoc->lh, fo.szName, &pObj->pObj, olerender_draw, 0);
  197.  
  198.         //If an embedded object fails, try static.
  199.         os=OsError(os, pDoc, pObj, TRUE);
  200.  
  201.         if (OLE_OK!=os)
  202.             {
  203.             os=OleCreateFromClip(PSZOLE(IDS_STATIC), (LPOLECLIENT)pObj,
  204.                                  pDoc->lh, fo.szName, &pObj->pObj, olerender_draw, 0);
  205.  
  206.             os=OsError(os, pDoc, pObj, TRUE);
  207.             }
  208.         }
  209.  
  210.  
  211.     CloseClipboard();
  212.  
  213.     /*
  214.      * Generally OleCreateFromClip will not return OLE_WAIT_FOR_RELEASE
  215.      * but we need to be prepared in case it does.
  216.      */
  217.     if (OLE_OK!=os)
  218.         {
  219.         PObjectFree(pDoc, pObj);
  220.         return FALSE;
  221.         }
  222.  
  223.     //This frees pObj on failure.
  224.     hWndT=HBlackBoxCreate(pGlob->hWnd, &fo, TRUE, pObj);
  225.  
  226.     if (NULL==hWndT)
  227.         return FALSE;
  228.  
  229.     //Update the size and image in the window.
  230.     SendMessage(hWndT, BBM_OBJECTNOTIFY, OLE_CHANGED, (LONG)pObj);
  231.     return TRUE;
  232.     }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. /*
  239.  * FEditConvertToStatic
  240.  *
  241.  * Purpose:
  242.  *  Converts any object into a static object that contains the same
  243.  *  image but cannot be activated in any way.  The data is still held in
  244.  *  OLECLI but the object is frozen.
  245.  *
  246.  * Parameters:
  247.  *  pGlob           LPGLOBALS to the global variable block.
  248.  *  pDoc            LPDOCUMENT containing OLE information.
  249.  *
  250.  * Return Value:
  251.  *  BOOL            TRUE if successful, FALSE otherwise.
  252.  */
  253.  
  254. BOOL FAR PASCAL FEditConvertToStatic(LPGLOBALS pGlob, LPDOCUMENT pDoc)
  255.     {
  256.     LPOLEOBJECT     pOLEObj;
  257.     LPOLEOBJECT     pOLEObjT;
  258.     LPOBJECT        pObj;
  259.     OLESTATUS       os;
  260.     char            szObject[128];
  261.     char            szName[128];
  262.  
  263.  
  264.     pObj=(LPOBJECT)SendMessage(pGlob->hWndLastActive, BBM_POBJECTGET, 0, 0L);
  265.  
  266.     GetAtomName(pObj->aName, szName, 128);
  267.  
  268.     //Create a temporary name for the converted object.
  269.     lstrcpy(szObject, PSZOLE(IDS_STATIC));
  270.  
  271.     os=OleObjectConvert(pObj->pObj, PSZOLE(IDS_STATIC), (LPOLECLIENT)pObj,
  272.                         pDoc->lh, szName, &pOLEObj);
  273.  
  274.     if (OLE_OK!=os)
  275.         return TRUE;
  276.  
  277.     //Delete the old object and wait on it.
  278.     os=OleDelete(pObj->pObj);
  279.  
  280.     if (OLE_OK!=OsError(os, pDoc, pObj, TRUE))
  281.         {
  282.         //Could not convert, so delete the new object.
  283.         os=OleDelete(pOLEObj);
  284.  
  285.         //OsError uses pObj->pObj for waiting.
  286.         pOLEObjT=pObj->pObj;
  287.         pObj->pObj=pOLEObj;
  288.  
  289.         OsError(os, pDoc, pObj, TRUE);
  290.  
  291.         pObj->pObj=pOLEObjT;
  292.         return FALSE;
  293.         }
  294.  
  295.     pObj->pObj=pOLEObj;
  296.     OleRename(pObj->pObj, szName);
  297.  
  298.     //Insure that we're marked as static.
  299.     pObj->dwType = OT_STATIC;
  300.     return TRUE;
  301.     }
  302.